﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Truextend.CoffeShop.Presenter.InternalCoffeShop;
using System.Data;
using Microsoft.SharePoint;

namespace Truextend.CoffeShop.Internal_Coffee_Shop
{
    public partial class Internal_Coffee_ShopUserControl : UserControl , IInternalCoffeeShop
    {
        InternalCoffeeShopPresenter presenter;

        protected void Page_Load(object sender, EventArgs e)
        {
            presenter = new InternalCoffeeShopPresenter(this);
            if(!IsPostBack){
                presenter.FillProducts();
            }
        }

        protected void InternalProductsGridView_SelectedIndexChanged(object sender, EventArgs e)
        {
            presenter.AddConsumption(InternalProductsGridView.SelectedDataKey.Value.ToString());
            presenter.FillProducts();
        }   

        #region Implements IInternalCoffeShop

        public System.Data.DataTable products
        {
            get
            {
                DataTable dataTable = new DataTable();

                dataTable.Columns.Add("ProductID");
                dataTable.Columns.Add("Product");
                dataTable.Columns.Add("Price");
                dataTable.Columns.Add("Amount");
                dataTable.Columns.Add("TotalPrice");
               
                for(int i = 0 ; i < InternalProductsGridView.Rows.Count ; i++){
                    
                    GridViewRow row = InternalProductsGridView.Rows[i];

                    DataRow dataRow = dataTable.NewRow();
                    dataRow["ProductID"] = this.InternalProductsGridView.DataKeys[i].Value.ToString();
                    dataRow["Product"] = (row.FindControl("LblProduct") as Label).Text;
                    dataRow["Price"] = (row.FindControl("LblPrice") as Label).Text;
                    dataRow["Amount"] = (row.FindControl("LblAmount") as Label).Text;
                    dataRow["TotalPrice"] = (row.FindControl("TxBxTotalPrice") as TextBox).Text;

                    dataTable.Rows.Add(dataRow);
                }                

                return dataTable;
            }
            set
            {
                InternalProductsGridView.DataSource = value;
                InternalProductsGridView.Columns[0].Visible = false;
                InternalProductsGridView.DataBind();
            }
        }

        public int userId
        {
            get
            {
                return SPContext.Current.Web.CurrentUser.ID;
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string userName
        {
            get
            {
                return SPContext.Current.Web.CurrentUser.LoginName;
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public int amount
        {
            get
            {
                return int.Parse(HiddenAmount.Value.ToString());
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public float totalPrice
        {
            get
            {
                return float.Parse(HideTotalPrice.Value.ToString());
            }
            set
            {
                throw new NotImplementedException();
            }
        }


        #endregion
             
    }
}
